package com.test.qpbgboot.schedule.controller;

import com.test.qpbgboot.base.response.JsonResult;
import com.test.qpbgboot.schedule.pojo.dto.OrderCreateParam;
import com.test.qpbgboot.schedule.pojo.dto.OrderQuery;
import com.test.qpbgboot.schedule.pojo.dto.OrderUpdateParam;
import com.test.qpbgboot.schedule.pojo.vo.OrderInfoVO;
import com.test.qpbgboot.schedule.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/schedule/order")
@Slf4j
@Api(tags = "schedule module")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/select")
    @ApiOperation(value = "订单查询")
    public JsonResult selectOrder(@Valid OrderQuery orderQuery) {
        log.debug("订单查询:orderQuery={}", orderQuery);
        List<OrderInfoVO> orderInfoVOList = orderService.orderSelect(orderQuery);
        return JsonResult.success(orderInfoVOList);
    }

    @PostMapping("/create")
    @ApiOperation(value = "订单创建")
    public JsonResult createOrder(@Valid @RequestBody OrderCreateParam orderCreateParam) {
        log.debug("订单创建:orderCreateParam={}", orderCreateParam);
        orderService.orderCreate(orderCreateParam);
        return JsonResult.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "订单更新")
    public JsonResult updateOrder(@Valid @RequestBody OrderUpdateParam orderUpdateParam) {
        log.debug("订单更新:orderUpdateParam={}", orderUpdateParam);
        orderService.orderUpdate(orderUpdateParam);
        return JsonResult.success();
    }

    @PostMapping("/delete/{orderId}")
    @ApiOperation(value = "订单删除")
    public JsonResult deleteOrder(@PathVariable @ApiParam(value = "订单id",
            name = "orderId", required = true) Long orderId) {
        log.debug("订单删除:orderId={}", orderId);
        orderService.orderDelete(orderId);
        return JsonResult.success();
    }

    @PostMapping("/pay/{orderId}")
    @ApiOperation(value = "订单账户内支付")
    public JsonResult payOrder(@PathVariable @ApiParam(value = "订单id",
            name = "orderId", required = true) Long orderId) {
        log.debug("订单账户内支付:orderId={}", orderId);
        orderService.orderPay(orderId);
        return JsonResult.success();
    }

    @GetMapping("/alipay/{orderId}")
    @ApiOperation(value = "订单支付宝支付")
    public void recharge(@PathVariable @ApiParam(value = "订单id",
                                     name = "orderId", required = true) Long orderId,
                         @ApiIgnore HttpServletResponse response) throws IOException {
        log.debug("订单支付宝支付:orderId={}", orderId);
        String formPayStr = orderService.orderAlipay(orderId);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(formPayStr);
        writer.flush();
        writer.close();
    }

    @PostMapping("/alipay-notify")
    @ApiOperation(value = "订单支付支付宝通知")
    public void rechargeNotify(@RequestParam Map<String, String> params) {
        log.debug("订单支付支付宝通知:params={}", params);
        orderService.orderAlipayNotify(params);
    }

    @PostMapping("/refund")
    @ApiOperation(value = "订单退款")
    public JsonResult refund(
            @RequestParam("id")
            @ApiParam(value = "订单id", name="id", required = true)
            Long id) {
        log.debug("订单退款:id={}", id);
        orderService.orderRefund(id);
        return JsonResult.success();
    }
}
